# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface

import scrapy
from scrapy.pipelines.images import ImagesPipeline

class Imgspipline(ImagesPipeline):
    # 1. 发送请求(下载图片, 文件, 视频,xxx)
    def get_media_requests(self, item, info):
      	# 获取到图片的url
        url = item['url']
        # 进行请求
        yield scrapy.Request(url=url)  # 直接返回一个请求对象即可

    # 2. 图片存储路径
    def file_path(self, request, response=None, info=None, *, item=None):
        # 给图片起名称
        file_name = item['url'].split("/")[-1]  # 用item拿到url
        # return file_name
        return './a/b/c/d/'+file_name

    # 数据提交给下一个管道
    def item_completed(self, results, item, info):
        return item